package com.utils;

import com.mapper.ProductMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperConfig {

    public static ProductMapper config() throws IOException {

        //读取resource下的核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

        //获取SqlSessionFactoryBuilder:构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSqlSessionFactory 工厂对象
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);//解析核心文件

        //获取SqlSession:执行对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //获取UserMapper接口的子类对象: 底层动态代理实现   (jdk动态代理)
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);

        return mapper;
    }
}
